package com.zmn.plat.business.impl.online.service.group.product.group;

import java.util.List;

import javax.annotation.Resource;

import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.business.interfaces.online.service.group.product.group.OnlineServiceGroupProductGroupBService;
import com.zmn.plat.model.entity.online.service.group.OnlineServiceGroup;
import com.zmn.plat.model.entity.online.service.group.product.group.OnlineServiceGroupProductGroup;
import com.zmn.plat.services.interfaces.online.service.group.OnlineServiceGroupService;
import com.zmn.plat.services.interfaces.online.service.group.product.group.OnlineServiceGroupProductGroupService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author linfeng
 * @description 在线客服分组产品组配置BService实现类
 * @modifier
 * @since 2021/03/30 11:02
 **/
@Service
public class OnlineServiceGroupProductGroupBServiceImpl implements OnlineServiceGroupProductGroupBService {
    @Resource
    private OnlineServiceGroupProductGroupService onlineServiceGroupProductGroupService;
    @Resource
    private OnlineServiceGroupService onlineServiceGroupService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    @SuppressWarnings("Duplicates")
    public void configProductGroup(List<OnlineServiceGroupProductGroup> list, String operator) {
        // 先删除该分组下的城市数据
        onlineServiceGroupProductGroupService.deleteByGroupIdAndBizType(list.get(0).getGroupId(), list.get(0).getBizType());

        list.forEach(e -> {
            e.setCreater(operator);
            e.setCreateTime(DateUtil.getNow());
            e.setUpdater(operator);
            e.setUpdateTime(DateUtil.getNow());
        });

        onlineServiceGroupProductGroupService.insertBatch(list);

        Integer groupId = list.get(0).getGroupId();
        OnlineServiceGroup onlineServiceGroup = onlineServiceGroupService.findByKey(groupId);
        onlineServiceGroup.setUpdater(operator);
        onlineServiceGroup.setUpdateTime(DateUtil.getNow());
        onlineServiceGroupService.updateByKey(onlineServiceGroup);
    }
}